package com.jindianjichi.subject.domain.handler.subject;

import com.jindianjichi.basic.entity.SubjectMultiple;
import com.jindianjichi.basic.service.SubjectMultipleService;
import com.jindianjichi.subject.common.enums.IsDeletedEnum;
import com.jindianjichi.subject.common.enums.SubjectInfoTypeEnum;

import com.jindianjichi.subject.domain.convert.MutilySubjectConverter;
import com.jindianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jindianjichi.subject.domain.entity.SubjectInfoBo;
import com.jindianjichi.subject.domain.entity.SubjectOptionBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class SubjectMutipleHandler implements SubjectInfoHandler{
    @Resource
    SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLY;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        List<SubjectAnswerBo> optionList = subjectInfoBo.getOptionList();
        List<SubjectMultiple>list=new ArrayList<>();
        optionList.forEach(item->{
            SubjectMultiple subjectMultiple=new SubjectMultiple();
            subjectMultiple.setSubjectId(subjectInfoBo.getId());
            subjectMultiple.setOptionType(Long.valueOf(item.getOptionType()));
            subjectMultiple.setOptionContent(item.getOptionContent());
            subjectMultiple.setIsCorrect(item.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
            list.add(subjectMultiple);
        });
        subjectMultipleService.insertBatch(list);
    }

    @Override
    public SubjectOptionBo queryById(Long subjectId) {
        SubjectMultiple subjectMultiple=new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        List<SubjectMultiple> mutiplyList = subjectMultipleService.queryByCondition(subjectMultiple);
        List<SubjectAnswerBo> answerBos = MutilySubjectConverter.instance.convertAnswerBolist(mutiplyList);
        SubjectOptionBo subjectOptionBo=new SubjectOptionBo();
        subjectOptionBo.setOptionList(answerBos);
        return subjectOptionBo;
    }
}
